package cn.yanhu.kuwanapp.my.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import androidx.fragment.app.Fragment
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.util.ext.dp
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.databinding.ActivityWithdrawRecordBinding
import cn.yanhu.kuwanapp.home.adapter.ViewPageAdapter
import cn.yanhu.kuwanapp.my.fragment.WithdrawRecordFragment
import cn.yanhu.kuwanapp.user.UserViewModel
import cn.yanhu.kuwanapp.utils.helper.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView

/**
 * 提现记录
 */
class WithdrawRecordActivity : BaseActivity<ActivityWithdrawRecordBinding, UserViewModel>() {

    private val titles = mutableListOf("全部", "收入", "提现")
    private val fragments = mutableListOf<Fragment>()

    override fun setContentView(): Int = R.layout.activity_withdraw_record


    override fun initData() {
        setStatusBarStyle(false)
        initWidgets()
    }

    private fun initWidgets() {
        initTab()
    }

    private fun initTab() {
        if (fragments.size > 0) return
        for (index in titles.indices) {
            fragments.add(WithdrawRecordFragment.newInstance(index))
        }
        val commonNavigator = CommonNavigator(mContext)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val clipPagerTitleView = ClipPagerTitleView(context)
                clipPagerTitleView.text = titles[index]
                clipPagerTitleView.textSize = 14f.dp
                clipPagerTitleView.textColor = Color.parseColor("#909090")
                clipPagerTitleView.clipColor = Color.parseColor("#FF6E28")
                clipPagerTitleView.setOnClickListener { mBinding.viewpager.currentItem = index }
                return clipPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.roundRadius = 4f.dp
                indicator.lineHeight = 32f.dp
                indicator.xOffset = 15f.dp
                indicator.yOffset = 8f.dp
                indicator.setColors(Color.parseColor("#FFF2EA"))
                return indicator
            }
        }
        commonNavigator.isAdjustMode = true
        mBinding.magicIndicator.navigator = commonNavigator
        mBinding.viewpager.adapter = ViewPageAdapter(this, fragments)
        mBinding.viewpager.offscreenPageLimit = 2
        ViewPagerHelper.bind2(mBinding.magicIndicator, mBinding.viewpager)
    }

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, WithdrawRecordActivity::class.java)
            context.startActivity(intent)
        }
    }
}